﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Betop.Lib.Core.Base;
using Betop.Lib.Enums;
using Betop.Lib.Data;
using Betop.Lib.Web;
using Betop.Lib.Core.Tree;



namespace Betop.Lib.Core.Dict
{
    public class DictBO:CommonOperate<DictInfo>
    {
        private DictDAL _dict;

        protected override BaseDAL DAL
        {
            get
            {
                return _dict;
            }
        }

        public DictBO()
            : base(TableName.Dict.ToString())
        {
            _dict = new DictDAL();
        }

        public DictBO(DBHelper db):this()
        {
            _dict.dbOper = base.dbOper = db;
        }
        public IEnumerable<DictInfo> GetList(int item,PagerSortSetting pagerSetting)
        {
            IEnumerable<DictInfo> list = _dict.GetList((int)item,pagerSetting);
            return list;
        }

        public IEnumerable<DictInfo> GetList(DictItem item)
        {
            IEnumerable<DictInfo> list = _dict.GetList((int)item, null);
            return list;
        }

        public IEnumerable<DictInfo> GetList(int item)
        {
            IEnumerable<DictInfo> list = _dict.GetList(item, null);
            return list;
        }

        public int GetMaxCode(int item)
        {
            return _dict.GetMaxCode((int)item);
        }
     
        public bool Exist(string strName, string strID, int upperID)
        {
            TreeBO<DictInfo> tree = new TreeBO<DictInfo>(TableName.Dict.ToString());
            bool flag = tree.Exist(upperID, strName, strID);
            return flag;
        }

        public bool ExistCode(int code, string strID, int upperID)
        {
            bool flag = _dict.ExistCode(code, strID, upperID);
            return flag;
        }
       
        //public void BindListControl(System.Web.UI.WebControls.ListControl ctl, DictItem dictItem)
        //{
        //    IEnumerable<DictInfo> list = GetList(dictItem);
        //    ctl.DataSource = list;
        //    ctl.DataTextField = "Name";
        //    ctl.DataValueField = "ID";
        //    ctl.DataBind();
        //}

        public void BindListControl(System.Web.UI.WebControls.ListControl ctl, int dictItem , string valueField)
        {
            IEnumerable<DictInfo> list = GetList(dictItem);
            ctl.DataSource = list;
            ctl.DataTextField = "Name";
            ctl.DataValueField = valueField;
            ctl.DataBind();
        }

        public void BindListControl(System.Web.UI.WebControls.ListControl ctl, int dictItem)
        {
            BindListControl(ctl, dictItem, "Code");
        }

        /// <summary>
        /// 给datatable加一列字典里的名称
        /// </summary>
        /// <param name="dt">源dt</param>
        /// <param name="item">字典项</param>
        /// <param name="addColName">被加列的名称</param>
        /// <param name="byName">源表中存Code列的名称</param>
        public void AddName(DataTable dt, DictItem item, string addColName, string byColName)
        {
            DataTable list = _dict.GetDataTable((int)item);
            dt.Columns.Add(addColName);
            foreach (DataRow dr in dt.Rows)
            {
                DataRow[] rows = list.Select("Code = " + dr[byColName]);
                if (rows.Length > 0)
                {
                    dr[addColName] = rows[0]["Name"];
                }
            }
        }

        /// <summary>
        /// 给集合列表中加上字典名称
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="list"></param>
        /// <param name="item"></param>
        /// <param name="setColName">被加列的名称</param>
        /// <param name="byColName">源表中存Code列的名称</param>
        public void SetName<T>(IEnumerable<T> list,DictItem item,string setColName,string byColName)
        {
            List<DictInfo> dicts = _dict.GetList((int)item);
            foreach (T info in list)
            {
                Type type = info.GetType();
                IEnumerable<DictInfo> tmps = dicts.Where(d => d.Code == (int)type.GetProperty(byColName).GetValue(info, null));

                string name = "";
                if (tmps.Count() > 0)
                {
                    name = tmps.First().Name;
                }
                info.GetType().GetProperty(setColName).SetValue(info, name, null);
            }
        }

        //public void AddNameByID(System.Data.DataTable dt, string idColumn, string nameColumn, DictItem dictItem)
        //{
        //    List<DictInfo> list = this.GetList(dictItem).ToList();

        //    dt.Columns.Add(nameColumn);
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        IEnumerable<DictInfo> infos = list.Where(data => data.ID == (int)dr[idColumn]);
        //        if (infos.Count() > 0)
        //        {
        //            dr[nameColumn] = infos.First().Name;
        //        }
        //        else
        //        {
        //            dr[nameColumn] = "";
        //        }
        //    }
        //}

       

        public string GetName(DictItem dictItem, int code)
        {
            return _dict.GetName((int)dictItem, code);
        }
    }
}
